package com.lhy;

class RunnableDemo implements Runnable {
    private Thread t;
    private String threadName;
    //创建类的有参构造
    RunnableDemo(String name){
        threadName = name;
        System.out.println("构造Creating" + threadName );
    }
    @Override
    public void run() {
        //线程创建方法
        System.out.println("开始Running" + threadName );
        try {
            for (int i = 4; i > 0 ; i--) {
                System.out.println("循环Thread: " + threadName + "," + i);
                //线程休眠
                Thread.sleep(50);
            }
        } catch (Exception e) {
            System.out.println("错误Thread" + threadName + "interrupted.");
        }
        System.out.println("最终输出Thread" + threadName + "exiting.");
    }
    public void start(){
        System.out.println("进行判断Starting" + threadName);
        if (t == null){
            t = new Thread(this,threadName);
            t.start();
        }
    }
}

public class Test67{
    public static void main(String[] args) {
        //线程一
        RunnableDemo R1 = new RunnableDemo("  Thread - 1");
        R1.start();
        //线程二
        RunnableDemo T2 = new RunnableDemo("  Thread - 2");
        T2.start();
    }
}